package top.caoayu;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.caoayu.configuration.BeanConfig;
import top.caoayu.controller.Multiple;
import top.caoayu.pojo.User;
import top.caoayu.service.impl.AopDemoServiceImpl;
import top.caoayu.service.impl.UserServiceImpl;

import java.util.List;


public class Main {

    public static void main(String[] args) {
        getBeanByXml();
        getBeanByJava();
        getBeanByAnnotation();
        multipleImpl();
        aopByXml();
        aopByAnnotation();
    }

    /*
     * 通过 xml 配置获取 bean
     * */
    public static void getBeanByXml() {
        System.out.println("get bean by xml");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("aspects.xml", "daos.xml", "services.xml", "demoAop.xml");
        UserServiceImpl service = context.getBean("userService", UserServiceImpl.class);
        context.getBean("userService");
        List<User> users = service.findAll();
//        service.findAll();
        users.forEach(v -> System.out.println("name: " + v.getName() + ", age: " + v.getAge()));
    }

    /*
     * 通过 BeanConfig 配置类来获取 bean
     * */
    public static void getBeanByJava() {
        System.out.println("get bean by java bean config");
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeanConfig.class);
        UserServiceImpl service = context.getBean("userService", UserServiceImpl.class);
        List<User> users = service.findAll();
        System.out.println(context.getBean("userService").getClass());
        users.forEach(v -> System.out.println("name: " + v.getName() + ", age: " + v.getAge()));

    }

    /*
     * 通过 @Service，@Repository 和 @Component 注解来获取 bean
     * */
    public static void getBeanByAnnotation() {
        System.out.println("get bean by annotation");
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("top.caoayu");
        UserServiceImpl service = context.getBean(UserServiceImpl.class);
        List<User> users = service.findAll();
        users.forEach(v -> System.out.println("name: " + v.getName() + ", age: " + v.getAge()));
    }

    /*
     * 接口 多个 bean 实现的注入方式
     * */
    public static void multipleImpl() {
        System.out.println("start multiple");
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("top.caoayu");
        Multiple bean = context.getBean(Multiple.class);
        bean.toEcho();
    }

    /*
     * xml 配置的 aop
     * */
    public static void aopByXml() {
        System.out.println("start aop by xml");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("aspects.xml", "daos.xml", "services.xml", "demoAop.xml");
        AopDemoServiceImpl bean = context.getBean(AopDemoServiceImpl.class);
        bean.method1();
        bean.method2();
        try {
            bean.method3();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public static void aopByAnnotation() {
        System.out.println("start aop by annotation");
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("top.caoayu");
        AopDemoServiceImpl bean = context.getBean(AopDemoServiceImpl.class);
        bean.method1();
        bean.method2();
        try {
            bean.method3();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}